package com.qf.service.impl;

import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * @author lixu
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {


    @Value("${aliyun.oss.endpoint}")
    private String endpoint;//类的成员变量
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    /**
     * 上传图片
     * @param size
     * @param bytes
     * @return
     */
    @Override
    public DtsStorage create(String ext, long size, byte[] bytes) throws Exception{
        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setType(ext);

        //时间戳  UUID
        String objectName = System.currentTimeMillis() + new Random().nextInt(100000) + "." + ext;
        //1:上传文件到OSS
        String url = AliyunOSSUtil.uploadImage(
                endpoint,
                accessKeyId,
                accessKeySecret,
                bucketName,
                objectName,
                bytes
        );
        dtsStorage.setKey(objectName);
        dtsStorage.setUrl(url);
        //TODO 真正写完
        //TODO 保存数据库
        return dtsStorage;
    }
}
